﻿using System;
using System.Collections.Generic;
using System.Text;
using Galaxy.Core;
using Galaxy.TestModule;
using Galaxy.ModuleInterface.Messages;

//using Galaxy.Core.Constructor;
using System.Xml.Serialization;
using System.IO;
using System.Net;
using System.Windows.Forms;
using Galaxy.ModuleInspector;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleTest
{
    [Serializable]
    class TestClass : ISerializable
    {

        private string TestStr;

        public TestClass(string Test) { TestStr = Test; }

        public TestClass(SerializationInfo info, StreamingContext context)
        {
            TestStr = info.GetString("TestStr");
        }

        public override string ToString()
        {
            return TestStr;
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("TestStr", TestStr);
        }

        #endregion
    }
    
    static class Program
    {
        static private CGalaxyCore Core = new CGalaxyCore();
        static private CMessageSender MessageSender = new CMessageSender();
        static private CTextWriterModule TextWriterModule =
            new CTextWriterModule(Console.Out, "qwe");
        static private CGalaxyModuleInspector ModuleInspector = new CGalaxyModuleInspector();


        [STAThread]
        static void Main(string[] args)
        {
            CoreTest();
            //TestClass Test = new TestClass("123");

            //BinaryFormatter Serializer = new BinaryFormatter();

            //MemoryStream Stream = new MemoryStream();

            //Serializer.Serialize(Stream, Test);

            //Stream.Position = 0;

            //Test = Serializer.Deserialize(Stream) as TestClass;

            Console.WriteLine("Работу закончил.");

            Console.ReadLine();
        }

        private static void CoreTest()
        {
            //ModulList MList = new ModulList();
            //MList.Add(new Module("Модуль.dll",
            //    new Class("CMessageSender", 1, 1f, 1d, "qwe"),
            //    new Class("CTextWriterModule")));
            //MList.Add(new Module("Ядро.dll",
            //    new Class("CGalaxyCore", "qwe", 1)));
            //MList.Add(new Module("Портал.dll",
            //    new Class("CTCPPortal", 4567),
            //    new Class("CUDPPortal", "255.255.255.255", 4568)));

            //Serialize(MList);

            Core.AddModule(MessageSender);
            Core.AddModule(TextWriterModule);
            Core.AddModule(ModuleInspector);

            Core.Start();

            Console.WriteLine();
            Console.WriteLine("Работа Галактики");
            Console.WriteLine();

            Console.WriteLine("Отправка сообщений");
            MessageSender.SendMessage("Test");
            MessageSender.SendInvokeMessage();
            MessageSender.SendMessage("Test 1", TextWriterModule);
            MessageSender.SendInvokeMessage(TextWriterModule);
            MessageSender.SendMessage(new CStopModuleMessage(MessageSender),
                MessageSender);
            MessageSender.SendMessage(new CStartModuleMessage(MessageSender),
                MessageSender);
            MessageSender.SendMessage(new CInitializeModuleMessage(MessageSender),
                MessageSender);

            Console.ReadLine();
            Console.WriteLine("Остановка Галактики");
            Console.WriteLine();

            ModuleInspector.Stop();
            Core.Stop();
        }
    }
}
